﻿package mortal.game.view.copy.escort
{
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.task.data.*;
    import mortal.mvc.core.*;

    public class EscortCopyEscortMiniView extends EscortCopyMiniViewBase
    {
        private var _carLife:GTextFiled;
        private var _data:SEscortPlayer;
        private var _secTimer:SecTimer;
        private var _hasNotFar:Boolean;
        private var _entityInfo:EntityInfo;
        private static var _instance:EscortCopyEscortMiniView;

        public function EscortCopyEscortMiniView()
        {
            return;
        }// end function

        private function onLinkText(event:TextEvent) : void
        {
            AIManager.gotoOrSendtoNpc(NpcConst.NPC_3003001);
            return;
        }// end function

        private function onSceneAddSelfCar(event:DataEvent) : void
        {
            this._entityInfo = event.data as EntityInfo;
            if (this._entityInfo)
            {
                if (!this._secTimer)
                {
                    this._secTimer = new SecTimer(1);
                }
                this._secTimer.reset();
                this._secTimer.start();
                this._secTimer.addListener(TimerType.ENTERFRAME, this.onTimeChange);
                this._entityInfo.addEventListener(EntityInfoEventName.UpdateLife, this.updateLife);
            }
            return;
        }// end function

        private function onTimeChange(param1:SecTimer) : void
        {
            var _loc_2:* = ThingUtil.entityUtil.getEntity(this._entityInfo.entityInfo.entityId);
            if (!_loc_2)
            {
                return;
            }
            var _loc_3:* = GeomUtil.calcDistance(_loc_2.x2d, _loc_2.y2d, RolePlayer.instance.x2d, RolePlayer.instance.y2d);
            if (_loc_3 > EscortCopyCache.FAR_PX)
            {
                if (!this._hasNotFar)
                {
                    MsgManager.showRollTipsMsg(Language.getString(35182));
                    this._hasNotFar = true;
                }
            }
            else
            {
                this._hasNotFar = false;
            }
            return;
        }// end function

        private function killSecTimer() : void
        {
            if (this._secTimer)
            {
                this._secTimer.stop();
                this._secTimer.dispose(true);
                this._secTimer = null;
            }
            return;
        }// end function

        private function updateLife(event:Event = null) : void
        {
            if (!this._data)
            {
                return;
            }
            var _loc_2:* = int(this._entityInfo.entityInfo.life / this._entityInfo.entityInfo.maxLife * 100);
            this._carLife.text = Language.getStringByParam(70072, _loc_2);
            if (_loc_2 <= 0)
            {
                this.killSecTimer();
                this._entityInfo.removeEventListener(EntityInfoEventName.UpdateLife, this.updateLife);
            }
            return;
        }// end function

        override protected function onEndTaskSuccess(param1:Object) : void
        {
            var _loc_2:* = param1 as TaskInfo;
            if (_loc_2.stask.group == ETaskGroup._ETaskGroupEscort)
            {
                DisplayUtil.removeMe(this._carLife);
                DisplayUtil.removeMe(_taskContent);
                DisplayUtil.removeMe(_submitTaskBtn);
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeave));
            }
            return;
        }// end function

        override public function update(param1:Object, ... args) : void
        {
            var _loc_5:int = 0;
            this._data = param1 as SEscortPlayer;
            _todayNum.htmlText = Language.getStringByParam(70063, this._data.todayNum, 3);
            _tfLeftTime.setLeftTime(this._data.leftSeconds);
            this.numTxt = CopyUtil.getEscortCopyCarNum(this._data.todayColorMap).arr;
            args = HTMLUtil.addColor(Language.getString(35183), ColorConfig.instance.getItemColorString(this._data.color));
            var _loc_4:* = HTMLUtil.addLink(Language.getString(70120), GlobalStyle.colorLv);
            _taskContent.htmlText = Language.getStringByParam(70113, args, _loc_4);
            if (this._entityInfo == null)
            {
                this._carLife.text = Language.getStringByParam(70072, "100");
            }
            else
            {
                _loc_5 = Math.ceil(this._entityInfo.entityInfo.life / this._entityInfo.entityInfo.maxLife * 100);
                this._carLife.text = Language.getStringByParam(70072, _loc_5);
            }
            _selectBg.graphics.clear();
            _selectBg.graphics.beginFill(ColorConfig.instance.getItemColor(this._data.color).intColor, 0.7);
            _selectBg.graphics.drawRoundRect(0, 0, 230, 25, 8, 8);
            _selectBg.graphics.endFill();
            _selectBg.y = this._data.color * 25 + 33;
            this._carLife.y = this._data.color * 25 + 35;
            this.addChildAt(_selectBg, 0);
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            var _loc_3:* = Cache.instance.task.getTaskByGroupInTaskDoing(ETaskGroup._ETaskGroupEscort, ETaskStatus._ETaskStatusHadComplete);
            if (_loc_3.length > 0)
            {
                _taskInfo = _loc_3[0];
                this.addChild(_submitTaskBtn);
            }
            return;
        }// end function

        override protected function configUI() : void
        {
            _bgName = ImagesConst.TaskTrack_Bg;
            _titleText = "";
            super.configUI();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._carLife = UIFactory.gTextField(Language.getStringByParam(70072, 100), 115, 90, 120, 20, this);
            _taskContent.configEventListener(TextEvent.LINK, this.onLinkText);
            Dispatcher.addEventListener(EventName.SceneAddSelfCar, this.onSceneAddSelfCar);
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, taskUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskDoingDel, this.onEndTaskSuccess);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            Dispatcher.removeEventListener(EventName.SceneAddSelfCar, this.onSceneAddSelfCar);
            NetDispatcher.removeCmdListener(ServerCommand.TaskUpdate, taskUpdateHandler);
            NetDispatcher.removeCmdListener(ServerCommand.TaskDoingDel, this.onEndTaskSuccess);
            if (this._entityInfo)
            {
                this._entityInfo.removeEventListener(EntityInfoEventName.UpdateLife, this.updateLife);
            }
            this.killSecTimer();
            this._carLife.dispose(param1);
            this._hasNotFar = false;
            this._data = null;
            this._entityInfo = null;
            this._carLife = null;
            return;
        }// end function

        public static function get instance() : EscortCopyEscortMiniView
        {
            if (!_instance)
            {
                _instance = new EscortCopyEscortMiniView;
            }
            return _instance;
        }// end function

        public static function get hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
